package com.coolxi.controller;

import com.coolxi.entity.Flower;
import com.coolxi.service.FlowerService;
import com.coolxi.upload.WebImgUpload;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Eoly on 2017/6/12.
 */
@Controller
@RequestMapping("/flowers")
public class FlowerActionController {

    @Autowired
    FlowerService flowerService;

    @RequestMapping("/add")
    public String addFlower(
            @Validated Flower flower,
            BindingResult result,
            MultipartFile img,
            HttpServletRequest req
    ) {

        if (result.hasErrors()) {
            return "flower/addPage";
        }

        if (!img.isEmpty()) {
            WebImgUpload webImgUpload = new WebImgUpload(img, req, "/resources/upload", true) {
                @Override
                protected void doCheckFail() {
                    throw new RuntimeException("图片格式不合法");
                }
            };
            try {
                webImgUpload.upload();
            } catch (IOException e) {
                throw new RuntimeException("上传失败");
            }
            flower.setFlowerImgAddress(webImgUpload.getUploadFileName());
        }

        flowerService.addFlower(flower);

        return "redirect:/flower/home.html";
    }

    @RequestMapping("/edit/{id}")
    public String editFlower(
            @PathVariable Integer id,
            MultipartFile img,
            @Validated Flower flower,
            BindingResult result,
            HttpServletRequest req
    ) {
        if (result.hasErrors()) {
            return "forward:/flower/edit/" + id + ".html";
        }

        flower.setFlowerId(id);

        if (!img.isEmpty()) {
            WebImgUpload webImgUpload = new WebImgUpload(img, req, "/resources/upload", true) {
                @Override
                protected void doCheckFail() {
                    throw new RuntimeException("图片格式不合法.");
                }
            };
            try {
                webImgUpload.upload();
            } catch (IOException e) {
                throw new RuntimeException("上传失败");
            }
            flower.setFlowerImgAddress(webImgUpload.getUploadFileName());
        }

        flowerService.editFlower(flower);

        return "redirect:/flower/home.html";
    }

    @RequestMapping("/delete/{id}")
    public String deleteFlower(
            @PathVariable Integer id
    ) {
        flowerService.deleteFlower(id);
        return "redirect:/flower/home.html";
    }
}
